package me.awei.toolbar.slice;

import me.awei.toolbar.ResourceTable;
import me.awei.toolbar.menu.OnMenuItemClickListener;
import me.awei.toolbar.snackbar.SnackBar;
import me.awei.toolbar.util.LogUtil;
import me.awei.toolbar.view.Toolbar;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.TabList;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.global.resource.Resource;
import ohos.utils.Sequenceable;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Toolbar toolbar;
    private DirectionalLayout root;
    private SnackBar snackbar;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        root = (DirectionalLayout) findComponentById(ResourceTable.Id_root);
        toolbar = (Toolbar) findComponentById(ResourceTable.Id_toolbar);
        findComponentById(ResourceTable.Id_snack_click_btn).setClickedListener(this::onClick);
        toolbar.setTitle("Toolbar Title");
        toolbar.setNavigationIcon(convertResToElement(getContext(),ResourceTable.Media_back_arrow_black));
        toolbar.setNavigationOnClickListener(component -> {
            LogUtil.loge("=== click navigation icon");
        });
        toolbar.setActionViewClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtil.loge("=== click actionView icon");
            }
        });
        toolbar.inflateMenu(ResourceTable.Layout_tab);
        toolbar.addMenuClickListener();//添加Menu点击事件，方法可提出来
        toolbar.setOnMenuItemClickListener(createToolbarMenuListener());//Menu 弹出后的Item点击事件回调
    }

    private Element convertResToElement(Context mContext, int drawableRes) {
        Element element = null;
        try {
            Resource resource = mContext.getResourceManager().getResource(drawableRes);
            element = new PixelMapElement(resource);
            return element;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return element;
    }

    private void showUndoSnackbar(final String text, final int index) {
        SnackBar.Builder builder = new SnackBar.Builder(getAbility(), root);
        builder.withOnClickListener(createUndoSnackbarListener(index))                    // 弹框按钮点击效果
                .withMessage(text)                                                        // 弹框文本信息
                .withActionMessage(getStringValue(ResourceTable.String_undo))             // 弹框按钮文本信息
                .withStyle(Color.getIntColor("#20c7c6"))                        // 弹框文本信息颜色设置
                .withBackgroundColorId(Color.getIntColor("#313131"))            // 弹框按钮颜色设置
                .withTypeFace(Font.DEFAULT)
                .withClearQueued()
                .withDuration(SnackBar.LONG_SNACK);                                        // 弹框持续时间设置
        snackbar = builder.show();
    }

    private String getStringValue(int resId) {
        String undo = "";
        try {
            undo = getResourceManager().getElement(resId).getString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return undo;
    }

    private SnackBar.OnMessageClickListener createUndoSnackbarListener(final int index) {
        return new SnackBar.OnMessageClickListener() {

            @Override
            public void onMessageClick(Sequenceable sequenceable) {
                if (snackbar == null) {
                    return;
                }
                snackbar.hide();

            }

        };
    }

    private OnMenuItemClickListener createToolbarMenuListener() {
        return new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(final Component item) {
                switch (item.getId()) {
                    case ResourceTable.Id_remove_tab_menu_item:

                        return true;
                    case ResourceTable.Id_add_tab_menu_item:

                        return true;
                    case ResourceTable.Id_settings_menu_item:

                        return true;
                    default:
                        return false;
                }
            }
        };
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_snack_click_btn:
                String text = getString(ResourceTable.String_cleared_tabs_snackbar);
                showUndoSnackbar(text, 0);
                break;
        }
    }
}
